$(function(){
    window.qcwd_userId = 0;
    window.api_ur = 'http://api.cjwsc.com/';

    //获取url参数
    (function ($) {
        $.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            /*console.log(name);
            console.log(reg);*/
            if (r != null) return unescape(r[2]); return null;
        }
    })(jQuery);


    /**
     * 
     *用户通过token登录
     * 
     */
    function getUserIdByToken() {
           var token = $.getUrlParam('token'),
               mac = $.getUrlParam('mac'),
               url = api_ur + 'token_login/getUserIdByToken?token=' + token;
                
           $.ajax({
                url: url,
                type: 'POST',
                dataType: 'jsonp',
                jsonp: "jsonpcallback",
                success:function(data) {
                    if(data.error===false) {
                         window.qcwd_userId = data.msg;   
                    } else {
                        /*layer.msg('获取用户信息失败!',{
                            time:2000
                        });*/
                    }
                }
            });    
    }

    getUserIdByToken();



    //优惠券
    function valumeData(){
       var clientApp = 4,order = 1,orderBy = 1;
       var url = 'http://api.cjwsc.com/coupon/coupon/topic_coupon_list?clientApp='+clientApp+'&order='+order+'orderBy='+orderBy;
       $.ajax({
            url: url,
            type: 'POST',
            dataType: 'jsonp',
            jsonp: "jsonpcallback",
            success:function(data) {
                console.log(data);
                if(parseInt(data.status) == 0){
                    var res = data.data.list;console.log(res);
                    var leng = res.length;
                    var $_li = $(".yhj-wrap>li"),$_li_n = $(".hongbao-number>li"),val=[],desc=[],couponId=[];

                    for(var i=0;i<leng;i++){
                        val[i] = res[i]['value'];
                        desc[i] = res[i]['description'];
                        couponId[i] = res[i]['couponId'];
                    }

                    $_li.each(function(index){
                        var _this = $(this);
                        if(index != 5){                            
                            _this.data("couponid",couponId[index]);
                            _this.find(".number").text(val[index]);
                            _this.find(".yhj-condition").text(desc[index]);
                        }
                    });

                    $_li_n.each(function(index){
                        var _this = $(this);
                        _this.data("couponid",couponId[index]);
                        _this.find(".number").text(val[index]);
                        _this.find(".yhj-condition").text(desc[index]);
                    });
                }
            }
        })
    }

    valumeData();

    function ajaxVal(couponid){        
         //$("#user_id").val() 
        if(parseInt(window.qcwd_userId) > 0) {
            var user_id = window.qcwd_userId,couponId = couponid;
            var url = 'http://api.cjwsc.com/coupon/coupon/bindUserCoupon?clientApp='+'4'+'&couponId='+couponId+'&userId='+user_id;
            $.ajax({
                url: url,
                type: 'POST',
                dataType: 'jsonp',
                jsonp: "jsonpcallback",
                success:function(data) {
                    if(data.status == 0){
                        layer.msg('领取成功!',{
                            time:2000
                        });
                    }else{
                        //layer.msg(data.message,{time:1000});
                        // layer.msg(data.message,{
                        //     time:2000
                        // });
                        if(couponId == ''){
                            layer.msg('优惠券已领完',{time:1000});
                        }else{
                            layer.msg(data.message,{
                                time:2000
                            }); 
                        }
                    }    
                }
            });
        }else{
            layer.msg('请登录',{
                time:2000
            });
        }
    }
    function valumeClick(){
        var $_li = $(".yhj-wrap>li"),$_li_n = $(".hongbao-number>li"),couponId;
        $_li.each(function(index){
            var _this = $(this);
            if(index != 5){
                _this.on('click',function(){
                   couponId = _this.data("couponid");
                   ajaxVal(couponId);
                });
            }        
        });
        $_li_n.each(function(index){
            var _this = $(this);
            if(index != 5){
                _this.on('click',function(){
                   couponId = _this.data("couponid");
                   ajaxVal(couponId);
                });
            }        
        });
    }
    valumeClick();
    //滚动数据
    //格式化现在时间yyyy-mm-dd hh:mm:ss
    function getDateT(num){
        var d,s;
        var mt = num+'000';  
        d = new Date(parseInt(mt)); 
        s = d.getFullYear() + "/";        //取年份  
        s = s + (d.getMonth() + 1) + "/";//取月份  
        s += d.getDate();         //取日期  
        //s += d.getHours() + ":";       //取小时  
        //s += d.getMinutes() + ":";    //取分  
        //s += d.getSeconds();         //取秒  
        return(s);
    }
    function scrollData(){
        var url = 'http://api.cjwsc.com/get_lottery_data';
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'jsonp',
            jsonp: "jsonpcallback",
            success:function(data) {
                if(data.error == false){
                    var res = data.msg.list,$_li = $(".ul-box li"),user = [],ranking = [],order = [],date = [];
                    for(var i=0;i<res.length;i++){
                        user[i] = res[i]['userTel'];
                        ranking[i] = res[i]['prize'];
                        order[i] = res[i]['invoiceid'];
                        date[i] = getDateT(res[i]['created']);
                    }
                    $_li.each(function(index){
                        var _this = $(this);
                        _this.find(".name").text(user[index]);
                        _this.find(".leve").text(ranking[index]);
                        _this.find(".orederNum").text(order[index]);
                        _this.find(".data").text(date[index]);
                    });
                }
            }
        })
    }
    scrollData(); 

    
    function clickToken(){
        $("#cjbottom").on("click",function(){
            var token = $.getUrlParam('token');
            //window.location.href = "http://m.cjwsc.com/lucky_draw/lottery/qcluckydraw?token="+token;
            if(token == null || token == '')
            {
                window.location.href = "http://m.cjwsc.com/lucky_draw/lottery/qcluckydraw";
                //layer.msg("请登录",{time:2000});
            }
            else
            {
                if(parseInt(window.qcwd_userId) > 0)
                {
                    window.location.href = "http://m.cjwsc.com/lucky_draw/lottery/qcluckydraw?token="+token;
                }
                else
                {
                    window.location.href = "http://m.cjwsc.com/lucky_draw/lottery/qcluckydraw";
                    //layer.msg("请登录",{time:2000}); 
                }
                
            }
        });
    }
    clickToken();

});